package com.hpin.mlxteacher.child;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.RecordDetail;
import com.hpin.mlxteacher.utils.AbDateUtil;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;

import java.util.Date;
//import com.umeng.analytics.MobclickAgent;

/**
 * 对孩子评价的页面
 * Created by Aaron on 15/9/1.
 */
public class RecordDetailActivity extends BaseActivity {

    private RatingBar rb_drink;
    private RatingBar rb_eat;
    private RatingBar rb_happy;
    private RatingBar rb_sleep;
    private RatingBar rb_shit;
    private RatingBar rb_pee;
    private EditText et_content;
    private String stuName;
    //    private String id;
    private String stuId;
    private int currentCheck;
    private Dialog dialog;
    private int check;
    private TextView tv_right;
    //    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.comment_detail);

        stuName = getIntent().getStringExtra("stuName");
        //孩子的id
        stuId = getIntent().getStringExtra("stuId");
        //是否已经提交过
        check = getIntent().getIntExtra("check", 0);
//        position = getIntent().getIntExtra("position", -1);

        initWidget();

        if (check == 1) {
            //已经提交过了 , 不可更改

            tv_right.setText("");

            rb_drink.setIsIndicator(true);
            rb_eat.setIsIndicator(true);
            rb_happy.setIsIndicator(true);
            rb_sleep.setIsIndicator(true);
            rb_shit.setIsIndicator(true);
            rb_pee.setIsIndicator(true);

            et_content.setFocusable(false);
            et_content.setFocusableInTouchMode(false);
        }

        getSingleRecoder();

    }

    /**
     * 获得该孩子的草稿记录
     */
    private void getSingleRecoder() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.token, ""));
        j.put("day", AbDateUtil.getStringByFormat(new Date(), AbDateUtil.dateFormatYMD));
        j.put("id", stuId);

        HttpUtil.setPostRequest("获取中...", this, Constant.WEB_ROOT + Constant.RECORDINFO,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            RecordDetail result = JSONObject.parseObject(str, RecordDetail.class);
                            if (result.code == Constant.SUCCESS) {

                                rb_drink.setRating(result.data.water);
                                rb_eat.setRating(result.data.eat);
                                rb_happy.setRating(result.data.happy);
                                rb_pee.setRating(result.data.pee);
                                rb_shit.setRating(result.data.shit);
                                rb_sleep.setRating(result.data.sleep);
                                currentCheck = result.data.state;

                                et_content.setText(result.data.content);

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);
                        }

                    }
                }, null);

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_right = (TextView) findViewById(R.id.tv_right);

        tv_title.setText(stuName + "的生活记录");
        tv_right.setText(getResources().getString(R.string.commit_record));

        rb_drink = (RatingBar) findViewById(R.id.rb_drink);
        rb_eat = (RatingBar) findViewById(R.id.rb_eat);
        rb_happy = (RatingBar) findViewById(R.id.rb_happy);
        rb_sleep = (RatingBar) findViewById(R.id.rb_sleep);
        rb_shit = (RatingBar) findViewById(R.id.rb_shit);
        rb_pee = (RatingBar) findViewById(R.id.rb_pee);

        et_content = (EditText) findViewById(R.id.et_content);

        iv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left: //返回

//                back();
                finish();

                break;

            case R.id.tv_right: //提交

//                LogUtil.e("tag", "rb_drinkRating=" + rb_drinkRating + ",rb_eatRating:" + rb_eatRating + ",rb_happyRating:" + rb_happyRating
//                             + ", rb_sleepRating: " + rb_sleepRating + ", rb_shitRating :" + rb_shitRating + ",rb_peeRating :" + rb_peeRating);

                if (CommonUtils.isNull(et_content.getText().toString().trim()) && rb_drink.getRating() == 0 && rb_eat.getRating() == 0
                        && rb_happy.getRating() == 0 && rb_sleep.getRating() == 0 && rb_shit.getRating() == 0 && rb_pee.getRating() == 0) {

                    showToast("您还未对该学生进行评价");

                    return;
                } else {
                    //给出提示
                    dialog = new AlertDialog.Builder(this).setTitle("提交").setMessage("提交后该条记录将无法更改").setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).setPositiveButton("提交", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            commitComment(rb_drink.getRating(), rb_eat.getRating(),
                                    rb_happy.getRating(), rb_sleep.getRating(), rb_shit.getRating(), rb_pee.getRating(), 1);
                        }
                    }).show();
                }

                break;

        }

    }

//    private void back() {
//
//        //已经提交过了 直接返回
//        if (currentCheck == 1) {
//            finish();
//            return;
//        }
//
//        //未做任何操作, 直接返回
//        if (CommonUtils.isNull(et_content.getText().toString().trim()) && rb_drink.getRating() == 0 && rb_eat.getRating() == 0
//                && rb_happy.getRating() == 0 && rb_sleep.getRating() == 0 && rb_shit.getRating() == 0 && rb_pee.getRating() == 0) {
//
//            finish();
//            return;
//        }
//
//        commitComment(rb_drink.getRating(), rb_eat.getRating(), rb_happy.getRating(), rb_sleep.getRating(), rb_shit.getRating(), rb_pee.getRating(), 0);
//
//    }

    /**
     * 提交评价
     *
     * @param drink
     * @param eat
     * @param happy
     * @param sleep
     * @param shit
     * @param pee
     */
    private void commitComment(float drink, float eat, float happy, float sleep, float shit, float pee, final int check) {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.token, ""));
        j.put("id", stuId);
        j.put("state", check);
        j.put("picUrl", "");
        j.put("eat", eat);
        j.put("happy", happy);
        j.put("pee", pee);
        j.put("shit", shit);
        j.put("water", drink);
        j.put("sleep", sleep);
        j.put("content", CommonUtils.isNull(et_content.getText().toString().trim()) ? "" : et_content.getText().toString().trim());
        j.put("day", AbDateUtil.getStringByFormat(new Date(), AbDateUtil.dateFormatYMD));

//        CommentSignleReq commentSignleReq = new CommentSignleReq();
//        commentSignleReq.token = sp.getString(Constant.token, "");
////        commentSignleReq.id = id;
//        commentSignleReq.id = stuId;
//        commentSignleReq.state = check;
//        commentSignleReq.picUrl = "";
//        commentSignleReq.eat = eat + "";
//        commentSignleReq.happy = happy + "";
//        commentSignleReq.pee = pee + "";
//        commentSignleReq.shit = shit + "";
//        commentSignleReq.water = drink + "";
//        commentSignleReq.sleep = sleep + "";
//        commentSignleReq.content = CommonUtils.isNull(et_content.getText().toString().trim()) ? "" : et_content.getText().toString().trim();
//        commentSignleReq.day = AbDateUtil.getStringByFormat(new Date(), AbDateUtil.dateFormatYMD);

        HttpUtil.setPostRequest("等待中...", this, Constant.WEB_ROOT + Constant.RECORDUPDATE,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        if (check == 0) {
                            finish();
                            return;
                        }

                        try {

                            org.json.JSONObject result = new org.json.JSONObject(str);

                            if (result.getInt("code") == Constant.SUCCESS) {
                                dialog.dismiss();
                                //提交成功
                                Intent data = new Intent();
                                data.putExtra("check", 1);
                                setResult(10801, data);
                                finish();

                            } else {
                                showToast(Constant.showErrorMsg(result.getInt("code"), RecordDetailActivity.this));
                            }

                        } catch (Exception e) {

                            e.printStackTrace();
                            showToast("提交失败!");
                        }

                    }
                }, new HttpUtil.FailListener() {
                    @Override
                    public void setOnFailListener() {

                        if (check == 0)
                            finish();

                    }
                });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());
    }

    //返回
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
